#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define LED_TRIGGER 	"/sys/devices/platform/leds-gpio/leds/red_led/trigger"
#define LED_BRIGHTNESS 	"/sys/devices/platform/leds-gpio/leds/red_led/brightness"
#define USAGE()         fprintf(stderr, "usage:\n" \
				" %s <on|off>\n"    \
				" %s <trigger> <type>\n",argv[0],argv[0]) 

int main(int argc ,char **argv)
{
	int fd1,fd2;
	printf("argc = %d\r\n",argc);
	/*校验传参*/
	if(2 > argc)
	{
		perror("argc error");
		exit(-1);
	}
	
	/*打开文件*/

	fd1 = open(LED_TRIGGER,O_RDWR);
	if(fd1 < 0)
	{
		perror("fd1 open error");
		exit(-1);
	}
	fd2 = open(LED_BRIGHTNESS,O_RDWR);
	if(fd2 < 0)
	{
		perror("fd2 open error");
		exit(-1);
	}
	
	/*根据参数控制LED*/
	if(!strcmp(argv[1],"on"))
	{
		write(fd1,"none",strlen("none"));
		write(fd2,"1",strlen("1"));
	}
	else if(!strcmp(argv[1],"off"))
	{
		write(fd1,"none",strlen("none"));
		write(fd2,"0",strlen("0"));
	}
	else if(!strcmp(argv[1],"trigger"))
	{	
		if(argc != 3)
		{
			USAGE();
			exit(-1);
		}
		write(fd1,argv[2],strlen(argv[2]));
	}
	else
	{
		USAGE();
	}
	exit(0);
}
